package com.easy.controller;

import com.easy.bean.Admin;
import com.easy.bean.Customer;
import com.easy.bean.Stylist;
import com.easy.service.CustomerService;
import com.easy.service.LoginService;
import com.easy.util.*;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@CrossOrigin//允许跨域访问
@RequestMapping("/login")
public class LoginController {

    @Autowired
    LoginService loginService;
    @Autowired
    CustomerService customerService;


    @GetMapping("/captcha")
    public Result captcha(){
        return Result.success(CaptchaUtil.getCaptcha());
    }

    @PostMapping("/customerlogin")
    public Result login(Customer customer,@RequestParam("captcha") String captcha,@RequestParam("key") String key){
        //验证码验证

        if (!CaptchaUtil.checkCaptcha(key,captcha)){
            return Result.error(500,"验证码错误");
        }
        //登录
        try{
            String token =loginService.login(customer);
            //删除掉验证码 可以写一个
            return Result.success("登陆成功",token);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(500,e.getMessage());
        }


    }
    @PostMapping("/adminlogin")
    public Result login(Admin admin,@RequestParam("captcha") String captcha,@RequestParam("key") String key){
        //验证码验证

        if (!CaptchaUtil.checkCaptcha(key,captcha)){
            return Result.error(500,"验证码错误");
        }
        //登录
        try{
            String token =loginService.adminlogin(admin);
            //删除掉验证码 可以写一个
            return Result.success("登陆成功",token);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(500,e.getMessage());
        }


    }
    @PostMapping("/stylistlogin")
    public Result login(Stylist stylist, @RequestParam("captcha") String captcha, @RequestParam("key") String key){
        //验证码验证

        if (!CaptchaUtil.checkCaptcha(key,captcha)){
            return Result.error(500,"验证码错误");
        }
        //登录
        try{
            String token =loginService.stylistlogin(stylist);
            //删除掉验证码 可以写一个
            return Result.success("登陆成功",token);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(500,e.getMessage());
        }


    }

    @GetMapping("/logininfo")
    public Result logininfo(HttpServletRequest request){
        String token = request.getHeader(SYS.TOKEN_AUTHORIZATION);
        Customer customer=loginService.logininfo(token);
        return Result.success(customer);
    }
    @GetMapping("/adminlogininfo")
    public Result adminlogininfo(HttpServletRequest request){
        String token = request.getHeader(SYS.TOKEN_AUTHORIZATION);
        Admin admin =loginService.adminlogininfo(token);
        return Result.success(admin);
    }
    @GetMapping("/stylistlogininfo")
    public Result stylistlogininfo(HttpServletRequest request){
        String token = request.getHeader(SYS.TOKEN_AUTHORIZATION);
        Stylist stylist =loginService.stylistlogininfo(token);
        return Result.success(stylist);
    }
    @PostMapping("/adminchangePassword")
    public Result adminChangePassword(HttpServletRequest request,
                                      @RequestBody Map<String, String> params) {
        String token = request.getHeader(SYS.TOKEN_AUTHORIZATION);

        // 从请求参数中获取旧密码和新密码
        String oldPassword = params.get("oldPassword");
        String newPassword = params.get("newPassword");

        if (oldPassword == null || newPassword == null) {
            return Result.error(500, "参数错误");
        }

        try {
            // 从token中获取管理员ID
            Object idObj = TokenUtil.getOption(token, SYS.TOKEN_KEY_ID);
            if (idObj == null) {
                return Result.error(500, "token异常");
            }
            int adminId = Integer.parseInt(idObj.toString());

            // 调用服务层修改密码
            boolean success = loginService.changeadminPassword(adminId, oldPassword, newPassword);
            if (success) {
                EmailUtil.sendEmail(loginService.adminlogininfo(token).getEmil(), "密码修改成功"+newPassword);

                return Result.success("密码修改成功");
            } else {
                return Result.error(500, "密码修改失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(500, e.getMessage());
        }
    }
    @PostMapping("/register")
    public Result add(@RequestBody Customer customer) {
        customer.setPassword(PasswordUtil.getEncryption(customer.getPassword()));
        Object obj = customerService.add(customer);
        return Result.success(obj);
    }

}

